//=============================================================================
// LazyParser.NET - C# Expression Parser for .NET 2.0 
//
// Copyright (c) 2008 Philippe Leybaert
//
// Permission is hereby granted, free of charge, to any person obtaining a copy 
// of this software and associated documentation files (the "Software"), to deal 
// in the Software without restriction, including without limitation the rights 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is 
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//=============================================================================

using System;

namespace Activa.LazyParser
{
	public enum TokenType 
	{ 
		Term, 
		Operator, 
		UnaryOperator, 
		TernaryOperator,
		TernaryOperator1,
		TernaryOperator2,
		FunctionCall , 
		LeftParen , 
		RightParen , 
        ArgumentSeparator
	}

	public enum OperatorAssociativity
	{
		Left,
		Right
	}

	internal class TokenDefinition
	{
		private readonly TokenType _tokenType = TokenType.Term;
		private readonly OperatorAssociativity _associativity = OperatorAssociativity.Left;
		private readonly string       _pattern;
		private readonly int          _precedence;

		private readonly bool _isPartial;

		private TokenEvaluator _evaluator = null;

		private TokenDefinition _alternate = null;
		private TokenDefinition _root = null;

		public TokenDefinition(TokenType tokenType)
		{
			_tokenType = tokenType;

			_isPartial = (tokenType == TokenType.TernaryOperator1 || tokenType == TokenType.TernaryOperator2);
		}

		public TokenDefinition(TokenType tokenType , string pattern) : this(tokenType)
		{
			_pattern = pattern;
		}

		public TokenDefinition(TokenType tokenType, int precedence , string pattern)  : this(tokenType)
		{
			_precedence = precedence;
			_pattern = pattern;
		}

		public TokenDefinition(TokenType tokenType, int precedence, OperatorAssociativity associativity, string pattern) : this(tokenType)
		{
			_precedence = precedence;
			_pattern = pattern;
			_associativity = associativity;
		}

		public TokenDefinition(TokenType tokenType, TokenEvaluator evaluator) : this(tokenType,(string)null)
		{
			_evaluator = evaluator;
		}

		public TokenDefinition(TokenType tokenType, int precedence, TokenEvaluator evaluator) : this(tokenType, precedence, (string)null)
		{
			_evaluator = evaluator;
		}

		public TokenType Type
		{
			get { return _tokenType;}
		}

		public string Pattern
		{
			get { return _pattern; }
		}

		public int Precedence
		{
			get { return _precedence; }
		}

		public OperatorAssociativity Associativity
		{
			get { return _associativity; }
		}

		public TokenEvaluator Evaluator
		{
			get { return _evaluator; }
			set { _evaluator = value; }
		}

		public bool IsPartial
		{
			get { return _isPartial; }
		}

		public TokenDefinition Alternate
		{
			get { return _alternate; }
			set { _alternate = value; }
		}

		public TokenDefinition Root
		{
			get { return _root; }
			set { _root = value; }
		}
	}
}
